<?php
/**
 * @Author: pan
 * @Date:   2020-11-01 11:46:51
 * @Content: use php_curl visit api 
 * @Last Modified by:   pan
 * @Last Modified time: 2020-11-01 12:49:25
 */
function api($url,$param=[],$method='GET',$header=[]){
    $opts=[
        //return string
        CURLOPT_RETURNTRANSFER=>TRUE,
        //connect time wuxian
        CURLOPT_CONNECTTIMEOUT=>0,
        // execute time 30s
        CURLOPT_TIMEOUT=>30,
        // no echo  header
        CURLOPT_HEADER=>FALSE,
        // avoid ssl verify  peer
        CURLOPT_SSL_VERIFYPEER=>FALSE,
        // avoid ssl hostname
        CURLOPT_SSL_VERIFYHOST=>FALSE,
        // set header info
        CURLOPT_HTTPHEADER=>$header
    ];
    switch (strtoupper($method)) {
        case 'GET':
            $opts[CURLOPT_URL]=$url.'?'.http_build_query($param);
            break;        
        case 'POST':
            $opts[CURLOPT_URL]=$url;
            $opts[CURLOPT_POST]=TRUE;
            //post fields
            $opts[CURLOPT_POSTFIELDS]=$param;
            break;
    }
    $ch=curl_init();
    curl_setopt_array($ch, $opts);
    $result=curl_exec($ch);
    curl_close($ch);
    return $result;
}